﻿using AutoMapper;
using GGetTestService.Common;
using GGetTestService.Common.ApiResult;
using GGetTestService.Common.SearchFilter;
using GGetTestService.Dto.Student;
using GGetTestService.Entity;
using GGetTestService.Extension;
using GGetTestService.Repository;
using GGetTestService.Repository.UnitOfWork;
using GGetTestService.Services.Interface;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Test.Util;

namespace GGetTestService.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class StudentController : ControllerBase
    {
        private readonly IStudentService _service;
        private readonly ITeacherService _teacherService;
        private readonly IMapper _mapper;
        private readonly Rootobject _root;
        private readonly IUnitOfWork _unitOfWork;
        private readonly IRepository<Student> _repository;

        public StudentController(IStudentService service, ITeacherService teacherService, IMapper mapper, Rootobject root, IUnitOfWork unitOfWork)
        {
            _service = service;
            _teacherService = teacherService;
            _mapper = mapper;
            _root = root;
            _unitOfWork = unitOfWork;
            _repository = _unitOfWork.GetRepository<Student>();
        }

        [HttpGet(nameof(GetStudents))]
        public async Task<ActionResult<ApiResult>> GetStudents()
        {
            var list = await _service.GetStudentListAsync();
            var newList = _mapper.Map<IEnumerable<StudentDto>>(list);
            return ApiResultHelper.Success(newList, newList.Count());
        }

        [HttpPost(nameof(AddStudent))]
        public async Task<ActionResult<ApiResult>> AddStudent(StudentAddDto addDto)
        {
            if (addDto == null)
            {
                return BadRequest();
            }

            var student = _mapper.Map<Student>(addDto);

            var isExistTeacher = await _teacherService.ExistAsync(student.TeacherId);
            if (!isExistTeacher)
            {
                return ApiResultHelper.Error($"不存在Id为{student.TeacherId}的教师");
            }
            await _service.AddStudentAsync(student);
            return ApiResultHelper.Success(student);
        }

        [HttpGet(nameof(UpdateStudent))]
        public async Task<ActionResult<ApiResult>> UpdateStudent()
        {
            Student stu = new Student()
            {
                Id = "51c0dd0f-b6ea-4d6d-a46f-122240d4a732".ObjToGuid(),
                Name = "猪八戒1"
            };

            var model = await _service.GetModelByIdAsync("51c0dd0f-b6ea-4d6d-a46f-122240d4a732".ObjToGuid());
            model.Name = "猪八戒";

            //await _service.SaveChangesAsync();//首先查询出实体进行跟踪，然后对跟踪的实体进行修改后保存，会更新指定的字段
            await _service.UpdateAsync(model);//会更新所有字段
            return ApiResultHelper.Success();
        }

        [HttpGet(nameof(GetStudentInnerJoinTeacher))]
        public async Task GetStudentInnerJoinTeacher()
        {
            await _service.GetStudentInnerJoinTeacher();
        }

        //[HttpPost(nameof(SearchStudent))]
        //public async Task<ActionResult<ApiResult>> SearchStudent(StudentSearchDto searchDto)
        //{
        //    var expression = SearchHelper.GenerateExpressionCondition<Student, StudentSearchDto>(searchDto);
        //    var list = await _service.GetListWithNoTrackingAsync(expression);
        //    return ApiResultHelper.Success(list);
        //}

        [HttpPost(nameof(SearchStudent))]
        public async Task<MessageModel<IEnumerable<Student>>> SearchStudent(StudentSearchDto searchDto)
        {
            var queryableYY = _repository.Queryable;

            StudentSearchModel search = new StudentSearchModel()
            {
                Name = "李白",
                Age = 17,
                TeacherId = Guid.NewGuid()
            };

            queryableYY.Search<Student, StudentSearchModel>(search);
            var ddd = Query.Search<Student, StudentSearchModel>(queryableYY, search);
            var llll = Query.GenerateExpressionConditionyy<Student, StudentSearchModel>(search);


            var pp = ddd.ToList();

            var expression = SearchHelper.GenerateExpressionCondition<Student, StudentSearchDto>(searchDto);
            var list = await _service.GetListWithNoTrackingAsync(expression);
            return MessageModel<IEnumerable<Student>>.Success("成功", list);
        }
    }
}